package gui.obu.alerts.panels;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class ObuSpeedLimitAlertPanelImpl extends AbstractAlertPanelImpl implements ObuSpeedLimitAlertPanel {

	private Label speedLimitLbl;
	private Label currentSpeedLbl;
	private Label speedLimit;
	private Label currentSpeed;
	private Label message;
	private Label difference;
	private Integer limit = null;
	private Integer speed = null;

	public ObuSpeedLimitAlertPanelImpl(Display display, Shell shell) {
		super(display, shell);
		
		speedLimitLbl = new Label(shell, SWT.NONE);
		currentSpeedLbl = new Label(shell, SWT.NONE);
		speedLimit = new Label(shell, SWT.NONE);
		currentSpeed = new Label(shell, SWT.NONE);
		message = new Label(shell, SWT.NONE);
		difference = new Label(shell, SWT.NONE);
	}
	
	@Override
	public void open() {
		super.open();
		
		speedLimitLbl.setBounds(33, 140, 90, 19);
		speedLimitLbl.setText("Speed Limit:");
		speedLimitLbl.setFont(new Font(display, "Arial", 12, SWT.ITALIC));
		
		currentSpeedLbl.setBounds(193, 140, 112, 19);
		currentSpeedLbl.setText("Current Speed:");
		currentSpeedLbl.setFont(new Font(display, "Arial", 12, SWT.ITALIC));
		
		speedLimit.setBounds(129, 140, 58, 19);
		speedLimit.setFont(new Font(display, "Arial", 12, SWT.ITALIC));
		
		currentSpeed.setBounds(311, 140, 58, 19);
		currentSpeed.setFont(new Font(display, "Arial", 12, SWT.ITALIC));
		
		message.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		message.setBounds(63, 170, 205, 24);
		message.setText("Please slow down by ");
		message.setFont(new Font(display, "Arial", 16, SWT.ITALIC));
		
		difference.setBounds(268, 170, 112, 24);
		difference.setFont(new Font(display, "Arial", 16, SWT.ITALIC));
		difference.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		
		shell.open();
		shell.layout();
	}
	
	private void setSpeedLimit(String limit) {
		this.speedLimit.setText(limit + " mph");
		this.speedLimit.update();
		calcDifference();
	}
	
	private void setCurrentSpeed(String speed) {
		this.currentSpeed.setText(speed + " mph");
		this.currentSpeed.update();
		calcDifference();
	}
	
	public void setSpeedLimit(Number limit) {
		this.limit = limit.intValue();
		if(this.speedLimit != null) {
			setSpeedLimit(limit.toString());
		}
	}
	
	public void setCurrentSpeed(Number speed) {
		this.speed = speed.intValue();
		if(this.currentSpeed != null) {
			setCurrentSpeed(speed.toString());
		}
	}
	
	private void calcDifference() {
		if(limit != null && speed != null) {
			Integer diff = (Integer)speed - (Integer)limit;
			difference.setText(diff.toString() + " mph");
			difference.update();
		}
	}
	
	@Override
	public void dispose() {
		super.dispose();
		speedLimitLbl.dispose();
		currentSpeedLbl.dispose();
		speedLimit.dispose();
		currentSpeed.dispose();
		message.dispose();
		difference.dispose();
	}
}
